package com.pengxun.manager.controller.system;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.UserinfoService;
import com.pengxun.manager.vo.ChangePasswordVO;
import com.pengxun.manager.vo.ResetPasswordEmailVO;
import com.pengxun.manager.vo.UserinfoSearchVO;
import com.pengxun.manager.vo.UserinfoValidateVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/userinfo")
public class UserinfoController  {

    @Resource
    private UserinfoService userinfoService;

    @Resource
    private LoginService loginService;

    


    @RequestMapping("/insert")
    public Result insert(@RequestBody Userinfo userinfo) {
        try {
            userinfoService.insert(userinfo);

        }catch (EastrocException e){
            return ResultUtils.renderError(e.getMessage());
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Userinfo userinfo) {
        try {
            userinfoService.update(userinfo);
        }catch (EastrocException e){
            return ResultUtils.renderError(e.getMessage());
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/updateCurrentUser")
    public Result updateCurrentUser(@RequestBody Userinfo userinfo) {
        try {
            Userinfo currentUser = loginService.getCurrentUser();
            userinfo.setUserId(currentUser.getUserId());
            userinfoService.update(userinfo);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/delete")
    public Result delete(Long userId) {
        try {
            userinfoService.delete(userId);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody UserinfoSearchVO userinfoSearchVO) {
        try {
            return ResultUtils.render(userinfoService.findByPage(userinfoSearchVO));
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/findByUserId")
    public Result findById(Integer userId) {
        try {
            return ResultUtils.render( userinfoService.findByUserId(userId));
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }


    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestBody ChangePasswordVO changePasswordVO) {
        try {
            return ResultUtils.render(userinfoService.updatePassword(changePasswordVO));
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestBody Userinfo userinfo) {
        try {
            userinfoService.update(userinfo);
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
        return ResultUtils.render();
    }

    /**
     * 获取认证码
     *
     * @param key
     * @return
     */
    @RequestMapping("/sendValidateCode")
    public Result sendValidateCode(String key) {
        try {
            userinfoService.sendValidateCode(key);
            return ResultUtils.render();
        } catch (EastrocException e) {
            return ResultUtils.renderError(e.getMessage());
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }

    @RequestMapping("/updateValidateInfo")
    public Result updateValidateInfo(@RequestBody UserinfoValidateVO userinfoValidateInfo) {
        try {
            userinfoService.updateValidateInfo(userinfoValidateInfo);
            return ResultUtils.render();
        } catch (EastrocException e) {
            return ResultUtils.renderError(e.getMessage());
        } catch (Exception e) {
            return ResultUtils.renderError(e.getMessage());
        }
    }


    @RequestMapping("/sendValidateEmail")
    public Result sendValidateEmail(String email){
        userinfoService.sendValidateEmail(email);
        return ResultUtils.render();
    }

    @RequestMapping("/doValidateEmail")
    public Result doValidateEmail(String key){
        userinfoService.doValidateEmail(key);
        return ResultUtils.render();
    }

    @RequestMapping("/sendResetPasswordEmail")
    public Result sendResetPasswordEmail(String email){
        userinfoService.sendResetPasswordEmail(email);
        return ResultUtils.render();
    }

    @RequestMapping("/doResetPasswordEmail")
    public Result doResetPasswordEmail(@RequestBody ResetPasswordEmailVO resetPasswordEmailVO){
        userinfoService.doResetPassword(resetPasswordEmailVO);
        return ResultUtils.render();
    }
    
}
